<template>
	<el-dialog :title="form.id ? '编辑计划' : '新增计划'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" detailFormDialog label-width="150px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="年" prop="contractYear">
						<el-date-picker type="year" placeholder="请选择年" v-model="form.contractYear" value-format="YYYY"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="当年回款状态" prop="paymentStatus">
						<el-select v-model="form.paymentStatus" placeholder="请选择回款状态">
							<el-option :label="item.label" :value="item.value" v-for="(item, index) in payment_status" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="实际合同金额(元)" prop="actualMoneyCurrentYear">
						<el-input-number :precision="2" :min="0" v-model="form.actualMoneyCurrentYear" placeholder="请输入实际合同金额"></el-input-number>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="开票金额" prop="invoiceAmountYear">
						<el-input precision="2" :min="0" v-model="form.invoiceAmountYear" placeholder="请输入开票金额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支出金额" prop="expenditure">
						<el-input precision="2" :min="0" v-model="form.expenditure" placeholder="请输入支出金额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="居间费" prop="agencyFee">
						<el-input precision="2" :min="0" v-model="form.agencyFee" placeholder="请输入居间费"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="年合同额" prop="contractAmountYear">
						<el-input precision="2" :min="0" v-model="form.contractAmountYear" placeholder="请输入年合同额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="审定（结算）金额" prop="contractSettlementAmount">
						<el-input precision="2" :min="0" v-model="form.contractSettlementAmount" placeholder="请输入审定（结算）金额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="已收账款" prop="receivedAmount">
						<el-input precision="2" :min="0" v-model="form.receivedAmount" placeholder="请输入已收账款"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应收账款" prop="receivableAmount">
						<el-input precision="2" :min="0" v-model="form.receivableAmount" placeholder="请输入应收账款"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="待收账款" prop="dueInAmount">
						<el-input precision="2" :min="0" v-model="form.dueInAmount" placeholder="请输入待收账款"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="未收账款" prop="uncollected">
						<el-input precision="2" :min="0" v-model="form.uncollected" placeholder="请输入未收账款"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="计划回款日期" prop="planReturnDate">
						<el-date-picker type="date" placeholder="请选择计划回款日期" v-model="form.planReturnDate" :value-format="dateStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="当年需回款额" prop="needReturnMoneyYear">
						<el-input precision="2" :min="0" v-model="form.needReturnMoneyYear" placeholder="请输入当年需回款额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="当年需实际回款额" prop="needRealReturnMoneyYear">
						<el-input precision="2" :min="0" v-model="form.needRealReturnMoneyYear" placeholder="请输入当年需实际回款额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="当年实际回款额" prop="realReturnMoneyYear">
						<el-input precision="2" :min="0" v-model="form.realReturnMoneyYear" placeholder="请输入当年实际回款额"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="当年剩余回款" prop="remainderMoneyYear">
						<el-input precision="2" :min="0" v-model="form.remainderMoneyYear" placeholder="请输入当年剩余回款"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ContractPlanFormDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/contractDetail';
import { Ref } from 'vue';

const currentContractId = inject<Ref<string>>('currentContractId')!;

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 定义字典
const { payment_status } = useDict('payment_status');

// 提交表单数据
const form = reactive({
	id: '',
	contractId: '',
	contractYear: '',
	actualMoneyCurrentYear: 0,
	invoiceAmountYear: 0,
	expenditure: 0,
	agencyFee: 0,
	contractAmountYear: 0,
	contractSettlementAmount: 0,
	receivedAmount: 0,
	receivableAmount: 0,
	dueInAmount: 0,
	uncollected: 0,
	planReturnDate: '',
	needReturnMoneyYear: 0,
	needRealReturnMoneyYear: 0,
	realReturnMoneyYear: 0,
	remainderMoneyYear: 0,
	paymentStatus: '',
});

// 定义校验规则
const dataRules = ref({
	// contractYear: [{ required: true, message: '年不能为空', trigger: 'blur' }],
	// actualMoneyCurrentYear: [{ required: true, message: '实际合同金额不能为空', trigger: 'blur' }],
	// invoiceAmountYear: [{ required: true, message: '开票金额不能为空', trigger: 'blur' }],
	// expenditure: [{ required: true, message: '支出金额不能为空', trigger: 'blur' }],
	// agencyFee: [{ required: true, message: '居间费不能为空', trigger: 'blur' }],
	// contractAmountYear: [{ required: true, message: '年合同额不能为空', trigger: 'blur' }],
	// contractSettlementAmount: [{ required: true, message: '审定（结算）金额不能为空', trigger: 'blur' }],
	// receivedAmount: [{ required: true, message: '已收账款不能为空', trigger: 'blur' }],
	// receivableAmount: [{ required: true, message: '应收账款不能为空', trigger: 'blur' }],
	// dueInAmount: [{ required: true, message: '待收账款不能为空', trigger: 'blur' }],
	// uncollected: [{ required: true, message: '未收账款不能为空', trigger: 'blur' }],
	// planReturnDate: [{ required: true, message: '计划回款日期不能为空', trigger: 'blur' }],
	// needReturnMoneyYear: [{ required: true, message: '当年需回款额不能为空', trigger: 'blur' }],
	// needRealReturnMoneyYear: [{ required: true, message: '当年需实际回款额不能为空', trigger: 'blur' }],
	// realReturnMoneyYear: [{ required: true, message: '当年实际回款额不能为空', trigger: 'blur' }],
	// remainderMoneyYear: [{ required: true, message: '当年剩余回款不能为空', trigger: 'blur' }],
	paymentStatus: [{ required: true, message: '当年回款状态不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDetailDialog = (id?: string) => {
	visible.value = true;
	form.id = '';
	form.contractId = currentContractId.value;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取contractDetail信息
	if (id) {
		getcontractDetailData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getcontractDetailData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDetailDialog,
});
</script>
